<?php
    require_once("config.php");

    Functions::PrintHeader("Log Viewer");
    
    if (AuthManager::CheckPermissions(AuthConstants::ADMINISTRATORS)) {
        if (isset($_GET['action']) && $_GET['action'] == "reproduce") {
            if (isset($_GET['id']) && is_numeric($_GET['id'])) {
                // make a call to the AuthManager to change the session information
                // set session variable for DEBUG_REPRODUCE so we can drop out of the session
                // determine the location where the problem occurred
                // ensure session change took effect
                // redirect to page where this occurred

            }
        }
        $filters = array();
        $query = "";
        
        // define some different filtering and sorting parameters
        if (isset($_GET['query'])) {
            $_GET['query'] = stripslashes($_GET['query']);
            $query = $_GET['query'];
        }
        else if (isset($_GET['id'])) {
            $query = "SELECT * FROM log WHERE id=$_GET[id];";
        }
        else {
            if (isset($_GET['userid']))
                $filters['userid'] = "userid=$_GET[userid]";
            if (isset($_GET['startdate']))
                $filters['startdate'] = "time > $_GET[startdate]";
            if (isset($_GET['stopdate']))
                $filters['stopdate'] = "time < $_GET[stopdate]";
            if (isset($_GET['errlike']))
                $filters['errlike'] = "error LIKE '%$_GET[errlike]%'";
            if (isset($_GET['msglike']))
                $filters['msglike'] = "message LIKE '%$_GET[errlike]%'";
            if (isset($_GET['page']))
                $filters['page'] = "page LIKE '%$_GET[page]%'";

            $order = "time DESC";
            if (isset($_GET['order'])) {
                $order = $_GET['order'];
                if (isset($_GET['direction']))
                    $order .= " " . $_GET['direction'];
                else
                    $order .= " " . 'DESC';
            }

            $limit = "LIMIT";
            if (isset($_GET['startpage'])) {
                $limit .= " " . $_GET['startpage'];
                if (isset($_GET['numtoshow']))
                    $limit .= ", " . $_GET['numtoshow'];
                else
                    $limit .= ", 25";
            }
            else 
                $limit .= " 25";

            $query = "SELECT * FROM log";
            if (count($filters) > 0)
                $query .= " WHERE " . implode(" AND ",$filters);
            $query .= " ORDER BY " . $order;
        }

        $logentries = DBM::FetchAllRows($query);

        if (count($logentries) == 0) {
            InfoMessage::Show("No Logs Found","No log entries were found...");
        }
        else if (count($logentries) == 1) {
            // show a detailed view:
            // redraw the error, print out information below
            echo "<h2>Message Rendering</h2>";
            $row = array_pop($logentries);
            ErrorMessage::Show($row['error'],$row['message']);
            ?> 
            <hr />
                <table class='cgltable'>
                    <thead>
                        <tr><th colspan='2'>Error Details</th></tr>
                    </thead>
                    <tr>
                        <td class='label'>Page</td>
                        <td><?=$row['page']?></td>
                    </tr>
                    <tr>
                        <td class='label'>Time and Date</td>
                        <td><?=date("F d, Y g:i A",$row['time'])?></td>
                    </tr>
                    <tr>
                        <td class='label'>User</td>
                        <td>
                        <?php
                            $query = "SELECT * FROM guests WHERE id=$row[userid];";
                            $userinfo = DBM::FetchRow($query);

                            if (isset($userinfo['email']) || isset($userinfo['name'])) {
                                echo $userinfo['email'] . " (" . $userinfo['name'] . ")"; 
                            }
                            else if ($row['userid'] == 0)
                                echo "<i>Anonymous User</i>";
                            else echo $row['userid'];
                        ?>
                        </td>
                    </tr>
                    <tr>
                        <td class='label'>QueryString</th>
                        <td>
                        <table cellspacing='0'>
                            <?php
                                $pieces = explode("&",$row['querystring']);
                                $arr = array();
                                foreach ($pieces as $piece) {
                                    $parts = explode("=",$piece);
                                    if (!empty($parts[0]))
                                        $arr[$parts[0]] = urldecode($parts[1]);
                                }
                                echo "<pre>" . print_r($arr,true) . "</pre>";
                            ?>
                        </table>
                        </td>
                    </tr>
                    <tr>
                        <td class='label'>Post Data</td>
                        <td><pre><?=$row['postdata']?></pre></td>
                    </tr>
                    <tr>
                        <td class='label'>Session Data</td>
                        <td><pre><?=$row['session']?></pre></td>
                    </tr>
                    <tr>
                        <td class='label'>Cookie Data</td>
                        <td><pre><?=$row['cookie']?></pre></td>
                    </tr>
                 </table>


            <?php
        }
        else {
            echo "<h2>Entries</h2>";
            echo "<table class='cgltable'><thead><tr>" . 
                "<th>ID</th>" .
                "<th>Page</th>" . 
                "<th>Time</th>" . 
                "<th>Error</th>" . 
                "<th>Message</th>" . 
                "<th>&nbsp;</th>" . 
            "</tr></thead>";

            foreach ($logentries as $entry) {
                echo "<tr>" .
                    "<td>$entry[id]</td>" . 
                    "<td>$entry[page]</td>" . 
                    "<td>" . date("F d, Y g:i A",$entry['time']) . "</td>" .
                    "<td>$entry[error]</td>" . 
                    "<td>" . (strlen($entry['message']) > 50 ? substr($entry['message'],0,50) . "..." : $entry['message']) . "</td>" . 
                    "<td><a href='log.php?id=$entry[id]'>view details</a></td>" .
                    "</tr>";
            }

            echo "</table>";
            
            ?>
            <h2>Custom Query</h2>
            <form method="GET" action="log.php">
            <textarea style='width:98%;' rows='10' name='query'><?=(isset($_GET['query']) ? $_GET['query'] : "")?></textarea>
            <br />
            <input type='submit' value='Search' />
            </form>
            <?php
        }
    }
    Functions::PrintFooter();
?>
